﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSCC.Common;
using System.Data;
using System.Data.Common;

namespace TSCC.DataAccess
{
    public class DA_Matrix
    {
        #region Protected Property
        protected List<field> fieldList = null;
        protected System.Data.Common.DbCommand cmd = null;
        protected Dictionary<string, DbType> tableSchema = null;
        protected List<string> whereList = null;
        #endregion


        /// <summary>
        /// 輸入SQL Statement 產生出 IDbCommand
        /// </summary>
        /// <param name="p_select">SQL Statement</param>
        protected void setCommand(string p_statement)
        {
            cmd = Common.DAL.GetCommand();
            cmd.CommandText = p_statement;
        }

        /// <summary>
        /// 輸入查詢語法，取回DataTable
        /// </summary>
        /// <param name="p_select">Select Statement</param>
        /// <returns></returns>
        protected DataTable getData(string p_statement, string p_table)
        {
            setCommand(p_statement);
            DataTable dt = Common.DAL.GetDataTable(cmd);
            dt.TableName = p_table;
            return dt;
        }

        /// <summary>
        /// 傳入 DBCommand，取回DataTable
        /// </summary>
        /// <param name="p_cmd">已含Select Statement</param>
        /// <returns></returns>
        protected DataTable getData(IDbCommand p_cmd)
        {
            return Common.DAL.GetDataTable(p_cmd);
        }

        protected DataTable getData()
        {
            return getData(cmd);
        }

        protected void createCommand(string p_statement)
        {
            cmd = Common.DAL.GetCommand();
            cmd.CommandText = p_statement;
        }

        protected DbType GetFieldDbType(string p_field)
        {
            return tableSchema[p_field];
        }

        protected virtual void FillFieldList(string[] p_valueArray, string[] p_parmArray, string p_fieldName)
        {
            if (fieldList == null)
            {
                fieldList = new List<field>();
            }
            for (int i = 0; i < p_valueArray.Length; i++)
            {
                fieldList.Add(new field(p_parmArray[i], GetFieldDbType(p_fieldName), p_valueArray[i].ToString()));
            }
        }

        public virtual void AddCriteria(string p_fieldName, string p_inString, string[] p_values)
        {
            var parms = p_values.Select((s, i) => string.Format("@{0}{1}", p_fieldName, i.ToString())).ToArray();
            var inclause = string.Join(",", parms);
            FillFieldList(p_values, parms, p_fieldName);
            AddWhereList(string.Format(" AND {0} {1} ({2})"
                             , p_fieldName, p_inString, inclause));
        }

        protected virtual void AddWhereList(string p_string)
        {
            if (whereList == null)
            {
                whereList = new List<string>();
            }
            whereList.Add(p_string);
        }

        protected DbParameter getParameter(object p_value, DbType p_type)
        {
            //DbParameter para = cmd.CreateParameter();
            //para.Value = p_value;
            //para.DbType = p_type;
            //para.ParameterName = string.Empty;
            return getParameter(p_value,p_type,string.Empty);
        }

        protected DbParameter getParameter(object p_value, DbType p_type, string p_parameterName)
        {
            DbParameter para = cmd.CreateParameter();
            para.Value = p_value;
            para.DbType = p_type;
            para.ParameterName = p_parameterName;
            return para;
        }
    }


}
